home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / nodelete / part01 / nodelete.c next >
C/C++ Source or Header  |  1990-10-15  |  8KB  |  355 lines

  1. /* NoDelete V1.35E (C) HooverSoft 1990
  2.  
  3.    cc NoDelete +L
  4.    ln NoDelete -lc32
  5.  
  6.  
  7. */
  8.  
  9.  
  10. #ifdef AZTEC_C             /* Works under Manx, convert it to Lattice */
  11.                            /* if you don't mind the hassle...             */
  12.  
  13. #include<intuition/intuition.h>
  14. #include<intuition/intuitionbase.h>
  15. #include<libraries/dos.h>
  16. #include<libraries/dosextens.h>
  17. #include<stdio.h>
  18. /* #define DEBUG 1 */
  19.  
  20. char bodystring[80],extensions[10][10], titlebak[80], title[80];
  21. ULONG answer, EXT_SET = 0L,  howmany = 0L;
  22. UBYTE *position, *rindex();
  23.  
  24. struct FileHandle *output, *Output();
  25.  
  26. struct NewWindow mywindow = {
  27.    100,0,   /* window XY origin relative to TopLeft of screen */
  28.    350,10,  /* window width and height */
  29.    0,1,  /* detail and block pens */
  30.    CLOSEWINDOW+VANILLAKEY, /* IDCMP flags */
  31.    WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+SIMPLE_REFRESH, /* other window flags */
  32.    NULL, /* first gadget in gadget list */
  33.    NULL, /* custom CHECKMARK imagery */
  34.    (UBYTE *)title,   /* window title */
  35.    NULL, /* custom screen pointer */
  36.    NULL, /* custom bitmap */
  37.    5,5,  /* minimum width and height */
  38.    640,110, /* maximum width and height */
  39.    WBENCHSCREEN   /* destination screen type */
  40.    };
  41.  
  42.  
  43. struct IntuiText body = {
  44.    1,0,
  45.    JAM1|COMPLEMENT,
  46.    30,16,
  47.    NULL,
  48.    NULL,
  49.    NULL
  50.    };
  51.  
  52. struct IntuiText fbody = {
  53.    1,0,
  54.    JAM1|COMPLEMENT,
  55.    30,5,
  56.    NULL,
  57.    (UBYTE *) "System tries to fool around...",
  58.    &body
  59.    };
  60.  
  61. struct IntuiText ok = {
  62.    1,0,
  63.    JAM1|COMPLEMENT,
  64.    6,3,
  65.    NULL,
  66.    (UBYTE *)   "Yes, Delete!",
  67.    NULL
  68.    };
  69.  
  70. struct IntuiText no = {
  71.    1,0,
  72.    JAM1|COMPLEMENT,
  73.    6,3,
  74.    NULL,
  75.    (UBYTE *)  "Oops, No Way!",
  76.    NULL
  77.    };
  78.  
  79.  
  80. struct IntuiMessage *msg, *GetMsg();
  81.  
  82. struct IntuitionBase *IntuitionBase;
  83. struct Window        *mw, *aw, *OpenWindow();
  84. struct Library       *DosBase;
  85. UBYTE                *filename;
  86. void                 *OpenLibrary();
  87. struct Task          *FindTask();
  88.  
  89. void NoDelete()
  90. {
  91. #asm
  92.    public   _LVOSetProtection
  93.  
  94.    movem.l  d0-d7/a0-a6,-(sp)
  95. #endasm
  96.    geta4();
  97. #asm
  98.    move.l   d1,_filename
  99. #endasm
  100.  
  101.    if(EXT_SET)
  102.       {
  103.  
  104.       position = rindex(filename, '.');
  105.       if(!position)
  106.          goto delete_anyway;
  107.       else
  108.          {
  109.          for(answer=0L; answer < howmany; answer++)         
  110.             {
  111.             if( (strcmp( (UBYTE *) position, (UBYTE *) extensions[answer])) != 0L)
  112.                continue;
  113.             else
  114.                goto request;
  115.             }
  116.          }
  117.       goto delete_anyway;     
  118.       }  
  119. request:
  120.    strcpy(bodystring, "Delete File `");
  121.    strcat(bodystring, (char *)filename);
  122.    strcat(bodystring, "'?");
  123.    body.IText = (UBYTE *) bodystring;
  124.    aw = (struct Window *) IntuitionBase->ActiveWindow;
  125.    answer = (long)AutoRequest(aw, &fbody, &ok, &no, NULL, NULL, 360L, 61L);
  126.    if(!answer)
  127.    {
  128.       ;
  129. #asm
  130.    movem.l  (sp)+,d0-d7/a0-a6
  131.    move.l   #1,d0
  132. #endasm
  133.    return;
  134.    }
  135.  
  136.    if(answer)
  137.    {
  138.  
  139. delete_anyway:
  140.       ;
  141. #asm
  142.  
  143.       move.l   _DosBase,a6    
  144.       move.l   #$70f7,d0         ;Restore old vector
  145.       move.w   d0,-72(a6)
  146.       move.l   #$60000092,d0
  147.       move.l   d0,-70(a6)
  148.  
  149.       move.l   _filename,d1
  150.       move.l   #0,d2
  151.       jsr      _LVOSetProtection(a6)
  152.  
  153.       move.l   _filename,d1
  154.       jsr      -72(a6)           ;call DeleteFile() 
  155. #endasm
  156.  
  157.       ;
  158.  
  159.       SetFunction(DosBase, -72L, &NoDelete);
  160.  
  161. /* install our function */
  162.  
  163.  
  164. #asm
  165.       movem.l  (sp)+,d0-d7/a0-a6
  166.       move.l   #1,d0
  167. #endasm
  168.       return;
  169.       }
  170.    }
  171.       
  172.  
  173. void quit(why)
  174. UBYTE *why;
  175. {
  176.    Write(output, why, (ULONG) strlen(why) );
  177.    SetTaskPri((struct Task *)FindTask(0L), 0L);
  178.    if(DosBase)
  179.    {
  180.       ;
  181. #asm
  182.       move.l   _DosBase,a6    
  183.       move.l   #$70f7,d0         ;restore old vector
  184.       move.w   d0,-72(a6)
  185.       move.l   #$60000092,d0
  186.       move.l   d0,-70(a6)
  187. #endasm
  188.       ;
  189.    }
  190.  
  191.    if (mw)
  192.       CloseWindow(mw);
  193.  
  194.    if (DosBase)   
  195.       CloseLibrary(DosBase);
  196.  
  197.    if (IntuitionBase)
  198.       CloseLibrary(IntuitionBase);
  199.  
  200.    exit(0L);
  201. }
  202.  
  203. /* test_nd() tests the DeleteFile vector to see if NoDelete is already 
  204.    installed. Returns zero if NoDelete is installed, one if it is not.
  205. */
  206. #asm
  207.       public   _test_nd
  208. _test_nd:
  209.  
  210.       move.l   _DosBase,a6
  211.       cmpi.l   #$60000092,-70(a6)
  212.       bne.s    _is_there            
  213.       moveq    #1,d0
  214.       bra.s    _end
  215. _is_there:
  216.       moveq    #0,d0
  217. _end:
  218.       rts   
  219. #endasm
  220.  
  221. void main(argc,argv)
  222. long argc;
  223. char *argv[];
  224.    {
  225.  
  226.    static UBYTE *msg1 = (UBYTE *)"Installing NoDelete...";
  227.    static UBYTE *msg2 = (UBYTE *)"done. NoDelete V1.35E (c) HooverSoft 1990\n\n";
  228.    long i;
  229.  
  230.    DosBase = (struct Library *) OpenLibrary("dos.library",0L);
  231.    output = Output();
  232.    strcpy(title, (UBYTE *) "NoDelete V1.35E (c) HooverSoft");
  233.    
  234.    if(argc > 11)
  235.       quit("Sorry, only 10 extensions allowed!\n");
  236.  
  237. /* Der erste Teil dieser If-Anweisung sucht nach allen
  238.    gueltigen Endungen, die zu finden sind.
  239. */
  240.    if(argc >= 2)
  241.       {
  242.       for(i=1; i<argc; i++)
  243.          {
  244.    
  245.          if(argv[i][0] == '.')
  246.             {
  247.             EXT_SET = 1L;
  248.             strcpy(extensions[howmany], argv[i]);
  249.             howmany++;
  250.             }
  251.          else
  252.           continue;
  253.  
  254. #ifdef   DEBUG
  255.          printf("Copied %s, Result: %s\n", argv[i], extensions[i-1L]);
  256. #endif   
  257.          }
  258.  
  259. #ifdef   DEBUG
  260.       printf("I found %ld extensions.\n", howmany);
  261. #endif         
  262.  
  263.  
  264. /* Found any extensions in command line? 
  265.    if we did, please display */
  266.       if(howmany)
  267.             strcat(title, (UBYTE *) " EXT SET");         
  268. /* Search for options */
  269.  
  270.       for(i=1; i<argc; i++)
  271.          {
  272. #ifdef DEBUG
  273.          printf("Scanning argv[%ld]: %s\n", i, (UBYTE *) argv[i] );
  274. #endif
  275.          if( (char) argv[i][0] == '-')
  276.             switch( (char) argv[i][1] )
  277.                {
  278.                case 'y':   if( atoi(argv[i+1]) <= 245L)
  279.                               mywindow.TopEdge = atoi(argv[i+1]);
  280.                            break;
  281.  
  282.                case 'p':   if(atoi(argv[i+1]) < 10L && atoi(argv[i+1]) > -20L)
  283.                               SetTaskPri((struct Task *)FindTask(0L),\
  284.                                           (long)atoi(argv[i+1]));
  285.                            else
  286.                               SetTaskPri((struct Task *)FindTask(0L), -5L);
  287.                            break;
  288.  
  289.                case default: break;
  290.                }
  291.             /* end switch */
  292.          /*end if */
  293.       /*end for */
  294.          }  
  295.                
  296.       }
  297.  
  298.    if(!howmany)
  299.       {
  300.       EXT_SET = 0L;
  301.       strcat(title, (UBYTE *) " NO EXT");
  302.       }
  303.  
  304. /* calculate dimension of TitleBar */
  305.  
  306.    mywindow.Width = ( strlen(title) * 8L) + 85L;
  307.    mywindow.LeftEdge = (640L-mywindow.Width) / 2L;
  308.  
  309.    IntuitionBase =  OpenLibrary("intuition.library",0L);
  310.    Write(output, msg1, (ULONG)strlen(msg1));
  311.  
  312.    if( !test_nd() )
  313.       {
  314.       Write(output, "NoDelete already installed.\n",28L);
  315.       CloseLibrary(DosBase);
  316.       CloseLibrary(IntuitionBase);
  317.       exit(0L);
  318.       }
  319.  
  320.    mw = OpenWindow(&mywindow);
  321.    if(!mw)
  322.       quit("Could not open Window.\n\n");
  323.  
  324.    Write(output, msg2, (ULONG)strlen(msg2));
  325.    SetFunction(DosBase, -72L, &NoDelete); 
  326.  
  327.    for(;;)
  328.       {
  329.       Wait(1L << mw->UserPort->mp_SigBit);
  330. getmsg:
  331.       msg = GetMsg(mw->UserPort);
  332.       ReplyMsg(msg);
  333.       if (msg->Class == CLOSEWINDOW)
  334.          quit("NoDelete V1.35E exiting.\n\n");
  335.       if(msg->Class == VANILLAKEY)
  336.          {
  337.          strcpy(titlebak, (UBYTE *) "Extensions: ");
  338.          for(i=0L; i < howmany ; i++)
  339.             {
  340.             strcat(titlebak, extensions[i]);
  341.             strcat(titlebak, " ");
  342.             }
  343.          SetWindowTitles(mw, titlebak);
  344.          Wait(1L << mw->UserPort->mp_SigBit);
  345.          SetWindowTitles(mw, title);
  346.          ReplyMsg( GetMsg(mw->UserPort) );
  347.          }
  348.  
  349.       else
  350.          continue;
  351.       }
  352.    }
  353. #endif   
  354.    
  355.